package com.sanjiahantang.ythshopping.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.sanjiahantang.ythshopping.R;
import com.sanjiahantang.ythshopping.activity.GoodsFilterActivity;
import com.sanjiahantang.ythshopping.adapter.category.CategorySubAdapter;
import com.sanjiahantang.ythshopping.model.CategoryInfo;
import com.sanjiahantang.ythshopping.model.CategoryInfoModel;
import com.sanjiahantang.shopping.uilibrary.button.FloatingActionButton;
import com.sanjiahantang.shopping.uilibrary.recycler.RecyclerViewHeader;
import com.sanjiahantang.shopping.uilibrary.views.NoScrollGridView;

import java.util.List;

import cn.lightsky.infiniteindicator.slideview.BaseSliderView;


public class CategorySubFragment extends Fragment implements
        View.OnClickListener, BaseSliderView.OnSliderClickListener {

    private static final String         ARG_PARAM1 = "param1";
    private CategoryInfoModel           mDatas;         //数据


    private View                        mRootView;
    private LinearLayout ll_catetwoList; //商品列表
    private RecyclerViewHeader          mRecyclerHeader;
    private CategorySubAdapter          mCategoryAdapter;
    private List<CategoryInfo>          mCategoryInfos;
    private ScrollView mScrollCate;



    private FloatingActionButton mFab;



    public static CategorySubFragment newInstance(CategoryInfoModel data) {
        CategorySubFragment fragment = new CategorySubFragment();
        Bundle args = new Bundle();
        args.putParcelable(ARG_PARAM1, data);
        fragment.setArguments(args);
        return fragment;
    }

    public CategorySubFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mDatas = getArguments().getParcelable(ARG_PARAM1);
            mCategoryInfos = mDatas.getData().getClassify();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.category_sub_fragment, container, false);
        mFab = (FloatingActionButton) mRootView.findViewById(R.id.fabCategory);
        mFab.hide();
        mFab.setOnClickListener(this);

        setupViews(mRootView);

        return mRootView;
    }

    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }

    private void setupViews(View view) {

        LayoutInflater inflater=LayoutInflater.from(getActivity());
        ll_catetwoList= (LinearLayout) view.findViewById(R.id.catetwolistCategory);
        mScrollCate= (ScrollView) view.findViewById(R.id.catetwoSVCategory);
        if (mCategoryInfos.size()==0)
        {
            mScrollCate.setVisibility(View.GONE);
        }else {
            mScrollCate.setVisibility(View.VISIBLE);
            for(final CategoryInfo cate:mCategoryInfos)
            {
                View twoview=inflater.inflate(R.layout.cate_two_item,null);
                TextView tv_twoname= (TextView) twoview.findViewById(R.id.tv_cti_twoname);
            /*RecyclerView mRecycler = (RecyclerView) twoview.findViewById(R.id.rvSubCategory);
            GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 3);
            mRecycler.setLayoutManager(layoutManager);*/
                NoScrollGridView mRecycler = (NoScrollGridView) twoview.findViewById(R.id.rvSubCategory);

                mCategoryAdapter = new CategorySubAdapter(getContext(),
                        R.layout.category_two_level_item_temporary, cate.getChild());

                catetrirdAdapter adapter=new catetrirdAdapter(cate.getChild());
                mRecycler.setAdapter(adapter);
                tv_twoname.setText(cate.getClassify_name());
                tv_twoname.setOnClickListener(new View.OnClickListener(){
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(getContext(), GoodsFilterActivity.class);

                        Bundle extras = new Bundle();
//        extras.putString("keyword", keyword);
                        extras.putString("id",cate.getClassify_id());
                        intent.putExtras(extras);

                        getContext().startActivity(intent);
                    }
                });
                ll_catetwoList.addView(twoview);
            }
        }

    }

    private void dispatch(String keyword){
        Intent intent = new Intent(getContext(), GoodsFilterActivity.class);

        Bundle extras = new Bundle();
        extras.putString("keyword", keyword);
        intent.putExtras(extras);

        startActivity(intent);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.fabCategory:

                mScrollCate.scrollTo(10, 10);
                mFab.hide();

                break;

        }
    }

    @Override
    public void onSliderClick(BaseSliderView slider) {
        dispatch(slider.getBundle().getString("extra"));
    }


    public class catetrirdAdapter extends BaseAdapter
    {
        List<CategoryInfo.childclass> thirdcate;
        LayoutInflater inflater;

        public catetrirdAdapter(List<CategoryInfo.childclass> thirdcate)
        {
            this.thirdcate=thirdcate;
            inflater=LayoutInflater.from(getContext());
        }

        @Override
        public int getCount() {
            return thirdcate.size();
        }

        @Override
        public Object getItem(int i) {
            return thirdcate.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(final int i, View view, ViewGroup viewGroup) {
            view=inflater.inflate(R.layout.category_two_level_item_temporary,null);
            TextView tv_tname= (TextView) view.findViewById(R.id.tvTwoLevelItem);
            tv_tname.setText(thirdcate.get(i).getClassify_name());
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dispatch(thirdcate.get(i).getClassify_name(),thirdcate.get(i).getClassify_id()+"");
                }
            });

            return view;
        }

        private void dispatch(String keyword,String id){
            Intent intent = new Intent(getContext(), GoodsFilterActivity.class);

            Bundle extras = new Bundle();
//        extras.putString("keyword", keyword);
            extras.putString("id",id);
            intent.putExtras(extras);

            getContext().startActivity(intent);
        }
    }

}
